package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.ResumeWorkService;
import cn.iocoder.yudao.hires.vo.ResumeWorkPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeWorkSaveReqVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.ResumeWorkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.ResumeWorkMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 简历-工作经历 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResumeWorkServiceImpl implements ResumeWorkService {

    @Resource
    private ResumeWorkMapper workMapper;

    @Override
    public Integer createWork(ResumeWorkSaveReqVO createReqVO) {
        // 插入
        ResumeWorkDO work = BeanUtils.toBean(createReqVO, ResumeWorkDO.class);
        workMapper.insert(work);
        // 返回
        return work.getId();
    }

    @Override
    public void updateWork(ResumeWorkSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkExists(updateReqVO.getId());
        // 更新
        ResumeWorkDO updateObj = BeanUtils.toBean(updateReqVO, ResumeWorkDO.class);
        workMapper.updateById(updateObj);
    }

    @Override
    public void deleteWork(Integer id) {
        // 校验存在
        validateWorkExists(id);
        // 删除
        workMapper.deleteById(id);
    }

    private void validateWorkExists(Integer id) {
        if (workMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "简历-工作经历不存在"));
        }
    }

    @Override
    public ResumeWorkDO getWork(Integer id) {
        return workMapper.selectById(id);
    }

    @Override
    public PageResult<ResumeWorkDO> getWorkPage(ResumeWorkPageReqVO pageReqVO) {
        return workMapper.selectPage(pageReqVO);
    }

}